using HoWave.Common;
using HoWave.Web.Shop;
using System;
using System.Data;
using System.Text;
using System.Web.Script.Services;
using System.Web.Services;
[ScriptService, WebService(Namespace = "http://tempuri.org/"), WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : Shop
{
	private static global::WebService _instance = null;
	private new Conn conn = new Conn();
	public static global::WebService instance
	{
		get
		{
			if (global::WebService._instance == null)
			{
				global::WebService._instance = new global::WebService();
			}
			return global::WebService._instance;
		}
	}
	[WebMethod]
	public int GetClothesParentClassID()
	{
		return this.conn.GetScalar("select top 1 ClassID from HLShop_Class where ParentID=0 and ClassName='服装服饰'", new IDbDataParameter[0]).ToString().ToInt32(new int[0]);
	}
	[WebMethod]
	public string GetChildrenClassIDByParentID(int parentid)
	{
		return this.conn.GetScalar("select dbo.GetChildrenClassIDByParentID(" + parentid + ")", new IDbDataParameter[0]).ToString().Trim(new char[]
		{
			','
		});
	}
	[WebMethod]
	public DataTable GetBuyedGoodsByUserID(int UserID)
	{
		StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.Append("select  oi.productid,oi.Quantity,sp.ClassID,sp.ProductName,sp.ProductSmallImageUrl ProductImg,sp.PUnit ");
		stringBuilder.Append("");
		stringBuilder.Append("from dbo.HLShop_OrderItem oi ");
		stringBuilder.Append("left join dbo.HLShop_Product sp ");
		stringBuilder.Append("on sp.ProductID=oi.productid ");
		stringBuilder.Append("where oi.orderformnum in(");
		stringBuilder.AppendFormat("select orderformnum from  dbo.HLShop_Order where userid={0} and deliverstatus=2", UserID);
		stringBuilder.Append(")order by oi.productid asc");
		DataTable dataTable = this.conn.GetDataTable(stringBuilder.ToString());
		int count = dataTable.Rows.Count;
		int i = 0;
		for (int j = 1; j < count; j++)
		{
			if (dataTable.Rows[j]["productid"].ToString() == dataTable.Rows[i]["productid"].ToString())
			{
				dataTable.Rows[i]["Quantity"] = dataTable.Rows[j]["Quantity"].ToString().ToInt32(new int[0]) + dataTable.Rows[i]["Quantity"].ToString().ToInt32(new int[0]);
				dataTable.Rows[j]["Quantity"] = 0;
			}
			else
			{
				i = j;
			}
		}
		for (i = 0; i < dataTable.Rows.Count; i++)
		{
			if (dataTable.Rows[i]["Quantity"].ToString() == "0")
			{
				dataTable.Rows[i].Delete();
			}
		}
		dataTable.AcceptChanges();
		return dataTable;
	}
	[WebMethod]
	public string GetRootClass()
	{
		DataTable dataTable = this.conn.GetDataTable("Select * From [HLShop_Class] Where ParentID=0");
		return global::WebService.DataSetToJson(dataTable);
	}
	[WebMethod]
	public string GetProductClassByParentID(int ParentID)
	{
		DataTable dataTable = this.conn.GetDataTable("Select * From [HLShop_Class] Where ParentID=" + ParentID);
		return global::WebService.DataSetToJson(dataTable);
	}
	[WebMethod]
	public string GetProductClassByClassID(int ClassID)
	{
		DataTable dataTable = this.conn.GetDataTable("Select * From [HLShop_Class] Where ClassID=" + ClassID);
		return global::WebService.DataSetToJson(dataTable);
	}
	[WebMethod]
	public string GetProductClassBySearch(string ClassName)
	{
		string text = "";
		string sql = "Select ClassID From [HLShop_Class] where ClassName like '%" + ClassName + "%'";
		DataTable dataTable = this.conn.GetDataTable(sql);
		for (int i = 0; i < dataTable.Rows.Count; i++)
		{
			object obj = text;
			text = string.Concat(new object[]
			{
				obj,
				this.conn.GetScalar("select [dbo].[GetChildrenClassIDByParentID](" + dataTable.Rows[i]["ClassID"] + ")", 1),
				dataTable.Rows[i]["ClassID"].ToString(),
				","
			});
		}
		if (text[text.Length - 1].ToString() == ",")
		{
			text = text.Substring(0, text.Length - 2);
		}
		DataTable dataTable2 = this.conn.GetDataTable("select ClassID,dbo.GetClassParentPath(ClassID) as ClassName from  [HLShop_Class] where ClassID in (" + text + ")");
		return global::WebService.DataSetToJson(dataTable2);
	}
	[WebMethod]
	public string GetProductIDBySearch(string ID)
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_Class where ClassID=" + ID);
		DataTable dataTable2 = this.conn.GetDataTable("select * from HLShop_Class  where ParentID=0 and ClassID in (" + dataTable.Rows[0]["ParentPath"] + ")");
		return global::WebService.DataSetToJson(dataTable2);
	}
	[WebMethod]
	public string GetAddProductClassUser()
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HL_UserInfo where UserID=" + base.user.UserID);
		DataTable dt = null;
		if (dataTable != null && dataTable.Rows.Count != 0)
		{
			if (!string.IsNullOrEmpty(dataTable.Rows[0]["CM_UsedClassID"].ToString()))
			{
				string str = dataTable.Rows[0]["CM_UsedClassID"].ToString();
				dt = this.conn.GetDataTable("select ClassID,dbo.GetClassParentPath(ClassID) as ClassName from  [HLShop_Class] where ClassID in (" + str + ")");
			}
		}
		return global::WebService.DataSetToJson(dt);
	}
	[WebMethod]
	public string GetSpecByID(int sp_id)
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_spec where sp_id=" + sp_id);
		return global::WebService.DataSetToJson(dataTable);
	}
	[WebMethod]
	public string GetSpec_ValueByID(int sp_id)
	{
		DataTable dataTable = this.conn.GetDataTable("select * from dbo.HLshop_spec_value where sp_id=" + sp_id);
		return global::WebService.DataSetToJson(dataTable);
	}
	[WebMethod]
	public string GetProSpec_valueByTypeName(int ClassID)
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_Class c  inner join HLShop_Type_Spec  t on  c.Attributes_Type=t.Attributes_Type where c.ClassID=" + ClassID);
		return global::WebService.DataSetToJson(dataTable);
	}
	[WebMethod]
	public string GetSpec_valueBySp_id(int sp_id)
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_spec s  inner join HLshop_spec_value v on v.sp_id=s.sp_id where s.sp_id=" + sp_id + " order by s.sp_id desc");
		return global::WebService.DataSetToJson(dataTable);
	}
	[WebMethod]
	public string GetSpecBySp_id(int sp_id)
	{
		DataTable dataTable = this.conn.GetDataTable("select * from HLShop_spec s where s.sp_id=" + sp_id);
		return global::WebService.DataSetToJson(dataTable);
	}
	public static string DataSetToJson(DataTable dt)
	{
		string text = string.Empty;
		string result;
		try
		{
			if (dt == null || dt.Rows.Count == 0)
			{
				result = "";
				return result;
			}
			text = "{";
			object obj = text;
			text = string.Concat(new object[]
			{
				obj,
				"'table",
				1,
				"':["
			});
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				text += "{";
				for (int j = 0; j < dt.Columns.Count; j++)
				{
					string text2 = text;
					text = string.Concat(new string[]
					{
						text2,
						"'",
						dt.Columns[j].ColumnName,
						"':'",
						dt.Rows[i][j].ToString(),
						"'"
					});
					if (j != dt.Columns.Count - 1)
					{
						text += ",";
					}
				}
				text += "}";
				if (i != dt.Rows.Count - 1)
				{
					text += ",";
				}
			}
			text += "]";
			text += "}";
		}
		catch (Exception ex)
		{
			throw new Exception(ex.Message);
		}
		result = text;
		return result;
	}
}
